package com.elock.intf.controller;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.elock.base.core.Filter;
import com.elock.base.core.Page;
import com.elock.base.core.Pageable;
import com.elock.base.core.controller.BaseController;
import com.elock.base.core.controller.ResultMsg;
import com.elock.base.core.util.ConvertUtil;
import com.elock.entity.Elock;
import com.elock.intf.service.IntfElockService;
import com.elock.service.CompanyInfoService;
import com.elock.service.ElockSaleOrgService;
import com.elock.service.SaleOrgBaseService;
import com.elock.service.SystemDictBaseService;

/**
 * Controller - 锁
 * @author 仁德
 */
@RestController("intfElockController")
@RequestMapping("/intf/elock")
public class ElockController extends BaseController {

	@Resource(name = "intfElockServiceImpl")
	private IntfElockService intfElockService;
	@Resource(name = "systemDictBaseServiceImpl")
	private SystemDictBaseService systemDictBaseService;
	@Resource(name = "saleOrgBaseServiceImpl")
	private SaleOrgBaseService saleOrgBaseService;
	@Resource(name = "elockSaleOrgServiceImpl")
	private ElockSaleOrgService elockSaleOrgService;
	@Resource(name = "companyInfoServiceImpl")
	private CompanyInfoService companyInfoService;

	/**
	 * 列表数据
	 */
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public @ResponseBody ResultMsg list(String companycode, String elockNum,
			String elockName, String saleOrgSn, Pageable pageable, ModelMap model) {

		if (ConvertUtil.isEmpty(companycode)) {
			return error("请传入企业编号");
		}
		if (!companyInfoService.exists(Filter.eq("company_code", companycode))) {
			return error("不存在企业编号为[" + companycode + "]的企业");
		}

		Object[] args = { companycode, elockNum, elockName, saleOrgSn, null };
		Page<Map<String, Object>> page = intfElockService.findPage(pageable, args);

		return success().addObjX(page);
	}

	/**
	 * 保存
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public @ResponseBody ResultMsg update(String companycode, Elock elock,
			String saleOrgSn) {

		intfElockService.saveUpdate(companycode, elock, saleOrgSn);

		return success();
	}
}
